package cn.da.shuai.cool.system.service.service.impl;

import cn.da.shuai.cool.common.expection.CoolServiceException;
import cn.da.shuai.cool.mybatis.service.impl.CoolServiceImpl;
import cn.da.shuai.cool.system.service.mapper.CoolJobMapper;
import cn.da.shuai.cool.system.model.entity.job.CoolJob;
import cn.da.shuai.cool.system.service.service.ICoolJobService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.quartz.CronExpression;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 定时任务调度服务实现类
 */
@Service
public class CoolJobServiceImpl extends CoolServiceImpl<CoolJobMapper,CoolJob> implements ICoolJobService {

    @Autowired
    private Scheduler scheduler;

    @Override
    public List<CoolJob> selectJobList(CoolJob job) {
        return this.list(getWrapper(job));
    }

    @Override
    public Page<CoolJob> selectJobPage(CoolJob job, Page<CoolJob> page) {
        return this.page(page, getWrapper(job));
    }

    private LambdaQueryWrapper<CoolJob> getWrapper(CoolJob job) {
        LambdaQueryWrapper<CoolJob> lqw = new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isNotBlank(job.getJobName()), CoolJob::getJobName, job.getJobName())
           .eq(StrUtil.isNotBlank(job.getJobGroup()), CoolJob::getJobGroup, job.getJobGroup())
           .eq(StrUtil.isNotBlank(job.getStatus()), CoolJob::getStatus, job.getStatus())
           .like(StrUtil.isNotBlank(job.getInvokeTarget()), CoolJob::getInvokeTarget, job.getInvokeTarget());
        return lqw;
    }

    @Override
    public CoolJob selectJobById(Long jobId) {
        return this.getById(jobId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int pauseJob(CoolJob job) throws CoolServiceException {
        Long jobId = job.getId();
        String jobGroup = job.getJobGroup();
        job.setStatus("1");
        int rows = this.updateById(job) ? 1 : 0;
        if (rows > 0) {
            // TODO: 实现暂停定时任务的逻辑
        }
        return rows;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int resumeJob(CoolJob job) throws CoolServiceException {
        Long jobId = job.getId();
        String jobGroup = job.getJobGroup();
        job.setStatus("0");
        int rows = this.updateById(job) ? 1 : 0;
        if (rows > 0) {
            // TODO: 实现恢复定时任务的逻辑
        }
        return rows;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteJob(CoolJob job) throws CoolServiceException {
        Long jobId = job.getId();
        String jobGroup = job.getJobGroup();
        int rows = this.removeById(jobId) ? 1 : 0;
        if (rows > 0) {
            // TODO: 实现删除定时任务的逻辑
        }
        return rows;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteJobByIds(Long[] jobIds) throws CoolServiceException {
        for (Long jobId : jobIds) {
            CoolJob job = this.getById(jobId);
            deleteJob(job);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int changeStatus(CoolJob job) throws CoolServiceException {
        int rows = 0;
        String status = job.getStatus();
        if ("0".equals(status)) {
            rows = resumeJob(job);
        } else if ("1".equals(status)) {
            rows = pauseJob(job);
        }
        return rows;
    }

    @Override
    public void run(CoolJob job) throws CoolServiceException {
        Long jobId = job.getId();
        String jobGroup = job.getJobGroup();
        CoolJob properties = selectJobById(job.getId());
        // TODO: 实现立即执行定时任务的逻辑
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertJob(CoolJob job) throws CoolServiceException {
        job.setStatus("0");
        int rows = this.save(job) ? 1 : 0;
        if (rows > 0) {
            // TODO: 实现新增定时任务的逻辑
        }
        return rows;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateJob(CoolJob job) throws CoolServiceException {
        CoolJob properties = selectJobById(job.getId());
        int rows = this.updateById(job) ? 1 : 0;
        if (rows > 0) {
            // TODO: 实现更新定时任务的逻辑
        }
        return rows;
    }

    @Override
    public boolean checkCronExpressionIsValid(String cronExpression) {
        return CronExpression.isValidExpression(cronExpression);
    }
}